package adv8.sax;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MySaxHandler extends DefaultHandler {

	@Override
	// 读到了文本节点
	public void characters(char[] ch, int start, int length) throws SAXException {
		String s = new String(ch, start, length);
		System.out.println("结点值:" + s);
	}

	@Override
	// 文档开始
	public void startDocument() throws SAXException {
		System.out.println("文档开始了");
		super.startDocument();
	}

	@Override
	// 元素开始
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		// if(qName.equals("book"))
		// {
		// b=new Book();
		// }
		// super.startElement(uri, localName, qName, attributes);
		System.out.println("元素开始了");
		System.out.println(localName + "," + qName);
		System.out.println("属性：");
		for (int i = 0; attributes != null && i < attributes.getLength(); i++) {
			System.out.println(attributes.getQName(i) + "," + attributes.getValue(i));

		}
	}

	@Override
	// 节点结束了
	public void endElement(String uri, String localName, String qName) throws SAXException {
		System.out.println(localName + "," + qName + "元素结束了");
		//
		// if(qName.equals("book"))
		// {
		// //list.add(b);
		// }
	}

	@Override
	// 文档结束
	public void endDocument() throws SAXException {
		System.out.println("文档结果了");
		super.endDocument();
	}

	// Book b=null;

}
